package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.SpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SpecificationServiceImpl implements SpecificationService {

    private static final Logger logger = LoggerFactory.getLogger(SpecificationServiceImpl.class);

    @Resource
    private TbSpecificationMapper tbSpecificationMapper;

    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageInfo<TbSpecification> listSpecifications(Integer pageNum, Integer pageSize,String specName) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> specifications = tbSpecificationMapper.listSpecifications(specName);
        PageInfo<TbSpecification> pageInfo = new PageInfo<>(specifications);
        logger.info("执行成功");
        return pageInfo;
    }


    @Transactional
    @Override
    public int insertSpecificationAndTbSpecificationOption(String specName, List<TbSpecificationOption> tbSpecificationOptions) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setSpecName(specName);
        int i = tbSpecificationMapper.insertSpecification(tbSpecification);
        Long id = tbSpecification.getId();
        for (TbSpecificationOption option : tbSpecificationOptions){
            option.setSpecId(id);
            tbSpecificationOptionMapper.insertTbSpecificationOption(option);
        }
        logger.info("执行成功");
        return 1;
    }

    @Override
    public TbSpecification getSpecificationById(Long id) {
        return tbSpecificationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbSpecificationOption> getTbSpecificationOptionsById(Long id) {
        return tbSpecificationOptionMapper.getTbSpecificationOptionsById(id);
    }

    @Transactional
    @Override
    public int updateSpecificationAndTbSpecificationOption(List<TbSpecificationOption> tbSpecificationOptions, String specName, Long id) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setId(id);
        tbSpecification.setSpecName(specName);
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        tbSpecificationOptionMapper.deleteTbSpecificationOptionBySpecId(id);
        for (TbSpecificationOption option : tbSpecificationOptions){
            option.setSpecId(id);
            tbSpecificationOptionMapper.insertTbSpecificationOption(option);
        }
        logger.info("执行成功");
        return 1;
    }

    @Transactional
    @Override
    public int deleteSpecificationBatch(Long[] ids) {
        tbSpecificationMapper.deleteSpecificationBatch(ids);
        tbSpecificationOptionMapper.deleteSpecificationOptionBySpecId(ids);
        logger.info("执行成功");
        return 1;
    }

    @Override
    public List<TbSpecification> getSpecs() {
        return tbSpecificationMapper.selectByExample(new TbSpecificationExample());
    }
}
